//
//  LockDeployCell.m
//  AILock
//
//  Created by 谭亚明 on 2020/7/14.
//  Copyright © 2020 tym. All rights reserved.
//

#import "LockDeployCell.h"
#import "UIView+ZFFrame.h"
#import "UserModel.h"
#import "FamilyModel.h"
#import "UIImageView+YPImage.h"

@interface LockDeployCell ()

@property(nonatomic,weak) TYMLabel *titLab;
@property(nonatomic,weak) TYMLabel*conLab;
@property(nonatomic,weak) UISwitch *switchBtn;
@property(nonatomic,weak) UIImageView *rightIamge;
@end

@implementation LockDeployCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initUI];
    }
    return self;
}

-(void)initUI{
    TYMLabel *label = [TYMLabel lb_text:@"" font:Font_Medium(14) color:rgba(74, 74, 74)];
    label.frame = CGRectMake(15,14, 100, 20);
    [self.contentView addSubview:label];
    self.titLab = label;
    
    TYMLabel *titLab = [TYMLabel lb_text:@"" font:Font_Regular(12) color:rgba(155, 155, 155)];
    titLab.numberOfLines = 0;
    [self.contentView addSubview:titLab];
    [titLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titLab);
        make.top.mas_equalTo(self.titLab.mas_bottom).offset(2);
        make.width.mas_equalTo(240);
    }];
    self.conLab = titLab;
    
    UISwitch *sithch = [[UISwitch alloc] initWithFrame:CGRectMake(ScreenWidth - 45 - 45, 14.5, 49, 31)];
    
    sithch.onTintColor = rgba(235, 238, 241);
    sithch.tintColor = rgba(235, 238, 241);
    [sithch addTarget:self action:@selector(switchOnAction:) forControlEvents:UIControlEventValueChanged];
    [self.contentView addSubview:sithch];
    self.switchBtn = sithch;
    
    UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_list_more"]];
    [self.contentView addSubview:image];
    [image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(self.contentView);
    }];
    image.hidden = YES;
    self.rightIamge = image;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    
    self.titLab.text = dataDic[@"title"];
    self.conLab.text = dataDic[@"content"];
    
    if ([dataDic[@"isOpen"] integerValue] < 2) {
        if (self.conLab.text.length == 0) {
            [self.titLab mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(15);
                make.centerY.mas_equalTo(self.contentView);
            }];
        }else{
            [self.titLab mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(15);
                make.top.mas_equalTo(14);
            }];
        }
        
       [self setChangeMessageBtn:[dataDic[@"isOpen"] integerValue]];
        self.rightIamge.hidden = YES;
        self.switchBtn.hidden = NO;
    }else{
        self.switchBtn.hidden = YES;
        if ([UserTool getCurrentFamily].familyRole == 1) {
            self.rightIamge.hidden = NO;
            [self.conLab mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(self.rightIamge.mas_left).offset(-2);
                make.centerY.mas_equalTo(self.contentView);
            }];
        }else{
            [self.conLab mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(self.contentView.mas_right).offset(-15);
                make.centerY.mas_equalTo(self.contentView);
            }];
            self.rightIamge.hidden = YES;
        }
    }
    
    //非家长不能操作
    if([UserTool getCurrentFamily].familyRole == 2){
        self.userInteractionEnabled = NO;
        [self setChangeMessageBtn:NO];
    }else{
        self.userInteractionEnabled = YES;
    }
}

-(void)setSysDict:(EquSubSysDict *)sysDict{
    
    self.switchBtn.hidden = YES;
    self.titLab.text = sysDict.dictName;
    self.conLab.textAlignment = NSTextAlignmentRight;
    
    if ([UserTool getCurrentFamily].familyRole == 1) {
        self.rightIamge.hidden = NO;
        [self.conLab mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.contentView);
            make.right.mas_equalTo(self.rightIamge.mas_left);
        }];
    }else{
        self.rightIamge.hidden = YES;
        [self.conLab mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.contentView);
            make.right.mas_equalTo(-15);
        }];
    }
    
    for (EquSubSysDict *dict in sysDict.subSysDictList) {
        if ([self.titLab.text isEqualToString:@"总音量"]) {
           if ([ _config.volumeGloba isEqualToString:dict.dictValue]) {
               self.conLab.text = dict.dictName;
               break;
           }
            
        }else if ([self.titLab.text isEqualToString:@"按键音"]) {
           if ([ _config.volumeWarning isEqualToString:dict.dictValue]) {
               self.conLab.text = dict.dictName;
               break;
           }
            
        }else if ([self.titLab.text isEqualToString:@"开锁音"]) {
           if ([ _config.volumeWelcome isEqualToString:dict.dictValue]) {
               self.conLab.text = dict.dictName;
               break;
           }
            
        }else if ([self.titLab.text isEqualToString:@"门铃音"]) {
           if ([ _config.volumeDoorbell isEqualToString:dict.dictValue]) {
               self.conLab.text = dict.dictName;
               break;
           }
        }
    }
}

//是否能用
-(void)setChangeMessageBtn:(BOOL)isUse{
    self.switchBtn.on = isUse;
    [self switchOnAction:self.switchBtn];
}

-(void)switchOnAction:(UISwitch*)sender{
    if (!sender.on) {
        self.switchBtn.thumbTintColor = rgba(155, 155, 155);
    }else{
        self.switchBtn.thumbTintColor = rgba(76, 132, 255);
    }
    
    if(self.changeSwitchBlock){
        self.changeSwitchBlock(sender.on);
    }
}


+(NSString*)identifier{
    return @"LockDeployCell";
}

+(CGFloat)cellForHeight{
    return 65;
}

@end
